function [va, vna, ve] = valfunc(ve0, v10, v20,s,p,PC,param,glob,options)
% inputs 
%p -- choice of p next period, conditional on adjustment
%c -- the value function evaluated at the grid points
%cdf -- distribution of pri ces. an equilibrium object.

%% first in the adjust case
% Compute flow payoff
Fa       = menufun('F',s,p,PC,param,glob,options);

% Create state vector for continuation value
sa = [max(p/(1+param.mu),min(s(:,1))),s(:,2)];

% Compute value if adjust (not accounting for menu cost) 
va      =  Fa + param.beta*ve0(sa);

%% then the not adjust case
% Compute flow payoff
Fna      = menufun('F',s,s(:,1),PC,param,glob,options);

% Compute value
sna = [max(s(:,1)/(1 + param.mu),min(s(:,1))), s(:,2)];
vna      = Fna + param.beta*ve0(sna);

% Compute probability of adjusting - still need to do!
% diff = va(sa) - vna(sna);

prob = param.xi;

% for the menu cost model
%prob    = unifcdf(diff, 0, param.phibar);
%expcost = .5*(min(diff, param.phibar).^2)/param.phibar; % conditional expectation times prob of adjustment

%prob = (diff > param.f) + param.xi*(diff < param.f);

%expcost = param.xi*(diff < param.f)*0 + (1-param.xi)*(diff > param.f)*param.f;

%% put it all together
%ve      = glob.PnK*(prob.*v10(sa) + (1-prob).*v20(sna) - expcost);
c1 = reshape(va.', glob.n(1), glob.n(2));
c2 = reshape(vna.', glob.n(1), glob.n(2));

v1 = griddedInterpolant(glob.K,glob.Z,c1, 'linear');   
v2 = griddedInterpolant(glob.K,glob.Z,c2, 'linear');   
 
ve      = glob.PnK*(prob*v1(sa) + (1-prob)*v2(sna));

